package com.atguigu.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Override
    protected BaseDao<Permission> getEntityDao() {
         return permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {
//        获取所有的权限
        List<Permission> permissionList=permissionDao.findAll();
//        根据角色id查询已分配的权限的权限id
        List<Long> permissionIds=rolePermissionDao.findPermissionIdByRoleId(roleId);
//        创建返回的List
        List<Map<String,Object>> returnList=new ArrayList<>();
        for (Permission permission : permissionList) {
            Map map=new HashMap();
            map.put("id",permission.getId());
            map.put("pId",permission.getParentId());
            map.put("name",permission.getName());
            if(permissionIds.contains(permission.getId())){
                map.put("checked",true);
            }
//            将map放到返回的list中
            returnList.add(map);
        }
        return returnList;
    }

    @Override
    public void assignPermission(Long roleId, Long[] permissionIds) {
//        根据角色id删除已分配权限的方法
        rolePermissionDao.deletedPermissionIdsByRoleId(roleId);
        for (Long permissionId : permissionIds) {
            if (permissionId!=null) {
                rolePermissionDao.addRoleIdAndPermission(roleId,permissionId);
            }
        }
    }

    @Override
    public List<Permission> getMenuPermissionByAdminId(Long userId) {
        List<Permission> permissionList=null;
        if(userId==1L){
//            若为超级管理员，获取所有权限
             permissionList=permissionDao.findAll();
        }else {
            permissionList=permissionDao.getMenuPermissionByAdminId(userId);
        }
//        将List转换成树形结构
        List<Permission> treeList = PermissionHelper.bulid(permissionList);
        return treeList;
    }

    @Override
    public List<Permission> findAllMenu() {
        //全部权限列表
        List<Permission> permissionList = permissionDao.findAll();
        if(CollectionUtils.isEmpty(permissionList)) return null;
        //构建树形数据,总共三级
        //把权限数据构建成树形结构数据
        List<Permission> result = PermissionHelper.bulid(permissionList);
        return result;

    }

    @Override
    public List<String> getPermissionCodesByAdminId(Long id) {
        List<String> permissionCodes=null;
        if(id==1){
            permissionCodes=permissionDao.getAllPermissionCodes();
        }else {
            permissionCodes=permissionDao.getPermissionCodesByAdminId(id);
        }
        return permissionCodes;
    }
}
